package com.ruoyi.statistics.service.impl;

import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.service.ILabOrgnizationService;
import com.ruoyi.statistics.domain.LabStatisticsBaseCollege;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Instrumentation;
import com.ruoyi.system.service.IInstrumentationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.statistics.mapper.LabStatisticsEquipmentMapper;
import com.ruoyi.statistics.domain.LabStatisticsEquipment;
import com.ruoyi.statistics.service.ILabStatisticsEquipmentService;

/**
 * 贵重仪器设备Service业务层处理
 * 
 * @author jun.wang
 * @date 2022-05-21
 */
@Service
public class LabStatisticsEquipmentServiceImpl implements ILabStatisticsEquipmentService {
	@Autowired
	private LabStatisticsEquipmentMapper labStatisticsEquipmentMapper;

	@Autowired
	private IInstrumentationService instrumentationService;
	@Autowired
	private ILabOrgnizationService orgService;

	/**
	 * 查询贵重仪器设备
	 * 
	 * @param id
	 *            贵重仪器设备ID
	 * @return 贵重仪器设备
	 */
	@Override
	public LabStatisticsEquipment selectLabStatisticsEquipmentById(Long id) {
		return labStatisticsEquipmentMapper.selectLabStatisticsEquipmentById(id);
	}

	/**
	 * 查询贵重仪器设备列表
	 * 
	 * @param labStatisticsEquipment
	 *            贵重仪器设备
	 * @return 贵重仪器设备
	 */
	@Override
	public List<LabStatisticsEquipment> selectLabStatisticsEquipmentList(
			LabStatisticsEquipment labStatisticsEquipment) {
		return labStatisticsEquipmentMapper.selectLabStatisticsEquipmentList(labStatisticsEquipment);
	}

	/**
	 * 新增贵重仪器设备
	 * 
	 * @param labStatisticsEquipment
	 *            贵重仪器设备
	 * @return 结果
	 */
	@Override
	public int insertLabStatisticsEquipment(LabStatisticsEquipment labStatisticsEquipment) {
		return labStatisticsEquipmentMapper.insertLabStatisticsEquipment(labStatisticsEquipment);
	}

	/**
	 * 修改贵重仪器设备
	 * 
	 * @param labStatisticsEquipment
	 *            贵重仪器设备
	 * @return 结果
	 */
	@Override
	public int updateLabStatisticsEquipment(LabStatisticsEquipment labStatisticsEquipment) {
		labStatisticsEquipment.setUpdateTime(DateUtils.getNowDate());
		return labStatisticsEquipmentMapper.updateLabStatisticsEquipment(labStatisticsEquipment);
	}

	/**
	 * 批量删除贵重仪器设备
	 * 
	 * @param ids
	 *            需要删除的贵重仪器设备ID
	 * @return 结果
	 */
	@Override
	public int deleteLabStatisticsEquipmentByIds(Long[] ids) {
		return labStatisticsEquipmentMapper.deleteLabStatisticsEquipmentByIds(ids);
	}

	/**
	 * 删除贵重仪器设备信息
	 * 
	 * @param id
	 *            贵重仪器设备ID
	 * @return 结果
	 */
	@Override
	public int deleteLabStatisticsEquipmentById(Long id) {
		return labStatisticsEquipmentMapper.deleteLabStatisticsEquipmentById(id);
	}

	@Override
	public List<LabStatisticsEquipment> selectLabStatisticsEquipmentByBaseCollegeId(Long baseCollegeId) {
		return labStatisticsEquipmentMapper.selectLabStatisticsEquipmentByBaseCollegeId(baseCollegeId);
	}

	@Override
	public void addEquipment(LabStatisticsBaseCollege baseCollege, List<LabOrgnization> labOrgnizations) {
		Long baseCollegeId = baseCollege.getId();
		deleteLabStatisticsEquipmentByBaseCollegeId(baseCollegeId);

		for (LabOrgnization labOrgnization : labOrgnizations) {

			Instrumentation query = new Instrumentation();
			query.setLabId(labOrgnization.getOrgId());
			List<Instrumentation> instrumentations = instrumentationService.selectInstrumentationList(query);

			for (Instrumentation instrumentation : instrumentations) {

				LabStatisticsEquipment equipment = new LabStatisticsEquipment();
				equipment.setBaseCollegeId(baseCollegeId);
				equipment.setOrgCode(labOrgnization.getOrgCode());

				equipment.setNumber(instrumentation.getAssetNo());
				equipment.setClassifyNumber(instrumentation.getAssetNameOpt());
				equipment.setName(instrumentation.getName());
				equipment.setModel(instrumentation.getModel());
				equipment.setSpecs(instrumentation.getSpec());
				equipment.setChargePerson(instrumentation.getContactName());

				insertLabStatisticsEquipment(equipment);
			}
		}
	}

	@Override
	public int deleteLabStatisticsEquipmentByBaseCollegeId(Long baseCollegeId) {

		return labStatisticsEquipmentMapper.deleteLabStatisticsEquipmentByBaseCollegeId(baseCollegeId);
	}

	@Override
	@Transactional
	public void importData(List<LabStatisticsEquipment> equipmentList, Long baseCollegeId) {
		this.deleteLabStatisticsEquipmentByBaseCollegeId(baseCollegeId);
		Map<String, LabOrgnization> orgMap = new HashMap<String, LabOrgnization>();
		for(LabStatisticsEquipment equipment : equipmentList){
			/*String departmentName = equipment.getDepartment();
			if(departmentName.indexOf("/")>0){
				departmentName = departmentName.substring(departmentName.lastIndexOf("/")+1);
			}
			if(orgMap.get(departmentName)==null){
				LabOrgnization orgQuery = new LabOrgnization();
				orgQuery.setOrgCode(departmentName);
				List<LabOrgnization> orgList = orgService.selectLabOrgnizationList(orgQuery);
				if(orgList.size()==0){
					throw new RuntimeException("所在单位没有在部门中录入:"+equipment.getDepartment());
				}
				orgMap.put(departmentName, orgList.get(0));
			}
			equipment.setBaseCollegeId(orgMap.get(departmentName).getOrgId());*/
			equipment.setBaseCollegeId(baseCollegeId);
			this.insertLabStatisticsEquipment(equipment);
		}
	}
}
